using System;
using System.Threading;
using System.Windows.Forms;
using robotManager.Helpful;
using wManager.Wow.Class;
using wManager.Wow.Helpers;
using wManager.Wow.ObjectManager;

public class Main : ICustomClass
{
    public float Range { get { return 4.5f; } } // Fight Range

    private bool _isLaunched;
    private const string FightClassName = "Interrupts";

    #region ListSpells

    private Spell _mightyBash;
    private Spell _skullBash;
    private string lastspell;

    #endregion ListSpells

    public void Initialize()
    {
        Logging.Write("Loading FightClass: " + FightClassName);

        // Init spells:
        _mightyBash = new Spell("Mighty Bash");
        _skullBash = new Spell("Skull Bash");
        Logging.Write("FightClass Loaded: " + FightClassName);

        // Launch loop in new thread:
        _isLaunched = true;
        var threadLoop = new Thread(Loop);
        threadLoop.Start();
        Logging.Write("FightClass Launched: " + FightClassName);
       
    }

    private void Loop()
    {
        while (_isLaunched)
        {
            try
            {
                if (Fight.InFight && ObjectManager.Target.IsValid)
                {
                    // Spell Mighty Bash:
                    if (ObjectManager.Target.IsCast && _mightyBash.IsSpellUsable && _mightyBash.IsDistanceGood && lastspell != "Skull Bash")
                    {
                        var resultLua =
                            Lua.LuaDoString(
                                "ret = \"false\"; spell, rank, displayName, icon, startTime, endTime, isTradeSkill, castID, interrupt = UnitCastingInfo(\"target\"); if interrupt then ret = \"true\" end",
                                "ret");
                        if (resultLua == "true")
                        {
                            _mightyBash.Launch();
                            lastspell = "Mighty Bash";
                        }
                    }
                }
                if (Fight.InFight && ObjectManager.Target.IsValid)
                {
                    // Spell Skull Bash:
                    if (ObjectManager.Target.IsCast && _skullBash.IsSpellUsable && _skullBash.IsDistanceGood && lastspell != "Mighty Bash")
                    {
                        var resultLua =
                            Lua.LuaDoString(
                                "ret = \"false\"; spell, rank, displayName, icon, startTime, endTime, isTradeSkill, castID, interrupt = UnitCastingInfo(\"target\"); if interrupt then ret = \"true\" end",
                                "ret");
                        if (resultLua == "true")
                        {
                            _skullBash.Launch();
                            lastspell = "Skull Bash";
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.Write("Error in FightClass " + FightClassName + ": " + e);
            }
            Thread.Sleep(35); // Wait for UC usage
        }
    }

    public void Dispose()
    {
        _isLaunched = false;
        Logging.Write("FightClass Disposed: " + FightClassName);
    }

    public void ShowConfiguration()
    {
        MessageBox.Show("No setting for this Fight Class: " + FightClassName);
    }
}